/**
 * Copyright (c) 2012-2013, Michael Yang 杨福海 (www.yangfuhai.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package pw.orm.utils;

import pw.orm.core.Property;
import pw.orm.core.Record;
import pw.orm.core.TableInfo;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;


public class RsUtils {

	public static <T> List<T> getEntities(ResultSet rs, Class<T> clazz){
		try {
			if(rs!=null){
				TableInfo table = TableInfo.get(clazz);
				ResultSetMetaData rsmd = rs.getMetaData();
				int columnCount = rsmd.getColumnCount();
				if(columnCount>0){
					List<T> results = new ArrayList<T>();
					while (rs.next()){
						T  entity =  clazz.newInstance();
						for(int i=1;i<=columnCount;i++){
							String column = rsmd.getColumnLabel(i);
							Property property = table.propertyMap.get(column);
							if(property!=null){
								property.setValue(entity, rs.getString(i));
							}else{
								if(table.getId().getColumn().equals(column)){
									table.getId().setValue(entity,  rs.getString(i));
								}
							}
						}
						results.add(entity);
					};
					return results;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	public static List<Record> getRecord(ResultSet rs){
		try{
			if(rs != null){
				ResultSetMetaData metaData = rs.getMetaData();

				int columnCount = metaData.getColumnCount();

				List<Record> results = new ArrayList<Record>();
				while (rs.next()){
					Record model = new Record();
					for(int i=1;i<=columnCount;i++){
						model.set(metaData.getColumnName(i), rs.getString(i));
					}
					results.add(model);
				};

				return results;
			}
		}catch (Exception e){
			e.printStackTrace();
		}

		return null;
	}
	
	
	public static <T> T record2Entity(Record record,Class<?> clazz){
		if(record!=null){
			try {
				@SuppressWarnings("unchecked")
				T  entity = (T) clazz.newInstance();
				HashMap<String,Object> dataMap = (HashMap<String,Object>)record;
				for(Entry<String, Object> entry : dataMap.entrySet()){
					String column = entry.getKey();
					TableInfo table = TableInfo.get(clazz);
					Property property = table.propertyMap.get(column);
					if(property!=null){
						property.setValue(entity, entry.getValue()==null?null:entry.getValue().toString());
					}else{
						if(table.getId().getColumn().equals(column)){
							table.getId().setValue(entity, entry.getValue()==null?null:entry.getValue().toString());
						}
					}
					
				}
				return entity;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	
}
